
# "2311.13314v2": {
#     "entryId": "2311.13314v2",
#     "title": "Sample Title 2",
#     "absText1": "Abstract Text 1",
#     "absText2": "Abstract Text 2",
#     "tags": [{"key": "tag2", "value": "value2"}]
# }

from app.tagabs import GetOriginAbs,RequestTranslatedAbs
from meta_service.store import ArxivPaper
from meta_service.tag import ArxivTag,InitTag
from meta_service.store import GetAllEntryIds

# 替换entry_id列表为指定的列表
replaced_entry_id_list = None
# replaced_entry_id_list = ['2407.14507v1', '2407.07071v1', '2406.11267v1', '2403.09037v2', '2403.01548v3', '2404.09841v2', '2312.15710v2', '2403.05262v2', '2405.17820v1', '2406.13230v1', '2403.01632v3', '2405.19325v2', '2406.17957v1', '2404.09002v1', '2408.02361v1', '2406.07735v1', '2405.10504v1', '2404.07060v1', '2401.12175v2', '2312.15576v1', '2401.10471v4', '2405.15356v1', '2402.03744v1', '2406.12663v1', '2402.15300v2', '2406.16477v1', '2401.14321v4', '2402.03407v1', '2405.11361v1', '2406.14283v4', '2405.10078v1', '2406.11357v2', '2404.09338v1', '2406.01920v1', '2405.15683v1', '2408.02032v1', '2406.00069v1', '2403.13838v1', '2402.01176v2', '2402.18476v1', '2404.09480v1', '2403.00425v2', '2401.05930v3', '2311.17911v3', '2404.00216v1', '2407.08039v1', '2407.07327v1', '2403.14401v1', '2403.00696v1', '2402.17982v1', '2406.09358v1', '2406.12718v2', '2407.03687v1', '2405.17821v1', '2407.06071v1', '2402.12654v2', '2406.12295v1', '2312.14335v2', '2407.15130v2', '2407.16908v1', '2406.18841v2', '2404.03865v1']

def get_all_entry_id() -> list[str]:
    from os import listdir
    from os.path import isfile, join
    all = GetAllEntryIds()
    # print(all)
    # replace entry_id list
    if replaced_entry_id_list is not None:
        ins = set(all) & set(replaced_entry_id_list)
        if len(ins) > 0:
            all = list(ins)
        else:
            raise Exception("elems of replaced_entry_id_list is not in all entry_id list")
    # sort
    all = sorted(all,reverse=True) # 保证最新的在最前面
    return all

def get_previous_entry_id(entry_id:str) -> str:
    entry_ids = get_all_entry_id()
    print(entry_id)
    if entry_id == "get_first_entry_id":
        return entry_ids[0]
    if entry_id in entry_ids:
        idx = entry_ids.index(entry_id)
        lenth = len(entry_ids)
        pre_idx = (idx + lenth - 1) % lenth
        return entry_ids[pre_idx]

def get_next_entry_id(entry_id:str) -> str:
    entry_ids = get_all_entry_id()
    if entry_id == "get_first_entry_id":
        return entry_ids[0]
    if entry_id in entry_ids:
        idx = entry_ids.index(entry_id)
        lenth = len(entry_ids)
        next_idx = (idx + 1) % lenth
        return entry_ids[next_idx]

def sync_vue_json_to_db(entry_id:str,tags:list[dict]) -> bool:
    if entry_id == "get_first_entry_id":
        return True
    try:
        tag = ArxivTag(entry_id)
        tag.ClearTag()
        # update tags
        for tag_dict in tags:
            tag.AddTag(tag_dict["key"],tag_dict["value"])
        return True
    except Exception as e:
        print("sync_vue_json_to_db error : " + str(e))
    return False

def build_vue_json_for_entry(entry_id:str) -> dict:
    data = None
    try:
        paper = ArxivPaper(entry_id)
        origin_abs = GetOriginAbs(entry_id)
        translated_abs = RequestTranslatedAbs(entry_id)
        title = paper.get_title()
        tag = ArxivTag(entry_id)
        tags = [ {"key":key,"value":val} for key,val in tag.GetTagDict().items() ]
        # build json
        data = {
            "entryId": entry_id,
            "title": title,
            "absText1": origin_abs,
            "absText2": translated_abs,
            "tags": tags
        }
    except Exception as e:
        print("build_vue_json_for_entry error : " + str(e))
        data = None
    return data

